package at.sysarch.filter;

import java.io.File;
import java.io.StreamCorruptedException;
import java.util.LinkedList;
import java.util.List;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import at.pimpmypipe.filter.AbstractSource;
import at.sysarch.bean.AbstractBean;
import at.sysarch.domain.TransferImage;

public class Source extends AbstractSource<TransferImage> {
	
	private File _file;
	private List<TransferImage> _images;
	
	public Source(AbstractBean<Source> abstractBean) {
		
		super(abstractBean);
	}

	@Override
	public TransferImage read() throws StreamCorruptedException {

		if (_images == null) {
			
			_images = new LinkedList<TransferImage>();
			
			PlanarImage image = JAI.create("fileload", _file.getAbsolutePath());
	
			TransferImage transfer = new TransferImage();
			
			transfer.setSourceImage(image);
			transfer.setModifiedImage(image);
			
			_images.add(transfer);
		}
		
		if (_images.size() > 0) {
			
			_bean.displayImage(_images.get(0));
			return _images.remove(0);
		}
		return null;
	}

	public File getFile() {
		return _file;
	}

	public void setFile(File file) {
		_file = file;
	}
}